#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>

int main(int argc, char *argv[])
{
    // 用户修改的变量
    int t = 5;                  // 音频时长, 单位秒
    int samplerate = 16000;     // 音频采样率
    double amplitude = 1.0;     // 最大赋值系数

    int i = 0;
    int n = 0;
    double a = 0;
    int cnt = 0;
    char *name = "sin.pcm";
    FILE *file = NULL;
    int16_t buf[16];

    n = samplerate / 1000;
    a = 3.1415926536 * 2 / n;
    printf("n = %d\n", n);

    for(i = 0; i < n; i++)
    {
        buf[i] = (int16_t)(sin(a * i) * 32767 * amplitude);
        printf("%hd, ", buf[i]);
    }
    printf("\n");

    file = fopen(name, "wb");
    if(!file)
    {
        printf("err: fopen\n");
        return -1;
    }

    for(i = 0; i < 1000 * t; i++)
    {
        cnt = fwrite(buf, 1, n * 2, file);
        if(cnt != n * 2)
        {
            printf("err: fwrite\n");
            return -1;
        }
    }

    fclose(file);
    return 0;
}
